/*
 *  This file is part of Cube.
 *
 *  Cube is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Cube is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Cube.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package cube.charts;


/**
 *
 * @author akermanl
 */
import java.awt.Color; 
import java.awt.Font; 
import java.util.Hashtable;
import org.jfree.chart.ChartPanel; 
import org.jfree.chart.JFreeChart; 
import org.jfree.chart.axis.DateAxis; 
import org.jfree.chart.axis.NumberAxis; 
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer; 
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; 
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RectangleInsets; 

public class LineChart extends Chart { 
 
    /*private TimeSeries x; 
    private TimeSeries y; 
    private TimeSeries z; */
        
    private XYSeriesCollection xyseriescollection;
    private Hashtable<String, XYSeries> axisHashtable = new Hashtable<String, XYSeries>();
    private XYPlot plot;

 
    public  LineChart(String title, int xPos, int yPos, int width, int height, String titleXAxis) {
        super(title, xPos, yPos, width, height);            
        
        xyseriescollection = new XYSeriesCollection();
        
        DateAxis domain = new DateAxis("time"); 
        //NumberAxis domain = new NumberAxis("");  
        NumberAxis range = new NumberAxis(titleXAxis);  
        
        XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false); 
        renderer.setSeriesPaint(0, Color.red); 
        renderer.setSeriesPaint(1, Color.green); 
        renderer.setSeriesPaint(2, Color.blue); 
        
        //XYPlot plot = new XYPlot(dataset, domain, range, renderer);         
        plot = new XYPlot(xyseriescollection, domain, range, renderer);
        
        //JFreeChart jfreechart = ChartFactory.createXYLineChart(null, "time", "g", null, PlotOrientation.VERTICAL, true, true, false);   
        //XYPlot plot = jfreechart.getXYPlot();
        //plot.setDataset(xyseriescollection);
                
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white); 
        plot.setRangeGridlinePaint(Color.white); 
        plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0)); 
        domain.setAutoRange(true); 
        domain.setLowerMargin(0.0); 
        domain.setUpperMargin(0.0); 
        domain.setTickLabelsVisible(true); 
        range.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 
        JFreeChart chart = new JFreeChart("", 
            new Font("SansSerif", Font.BOLD, 24), plot, true); 
        chart.setBackgroundPaint(Color.white); 
        ChartPanel chartPanel = new ChartPanel(chart); 
        //chartPanel.setBorder(BorderFactory.createCompoundBorder( 
        //BorderFactory.createEmptyBorder(4, 4, 4, 4), 
        //BorderFactory.createLineBorder(Color.black))); 
        add(chartPanel); 
        
        super.setContentPane(chartPanel);
    } 

      
    public void resetValues(){
        for (XYSeries xyseries : axisHashtable.values() ) {
             xyseries.clear();
        } 
    }
    
    public void updateValue(String id, double x, double y){
        axisHashtable.get(id).add(x,y);        
    }
    
    public void addAxis(String id, String name){        
        XYSeries xyseries = new XYSeries(name, true, false); 
        xyseries.setMaximumItemCount(200);
        axisHashtable.put(id, xyseries);
        reloadChart();
    }
    
    private void reloadChart(){
        xyseriescollection = new XYSeriesCollection();           
        for (XYSeries xyseries : axisHashtable.values() ) {
             xyseriescollection.addSeries(xyseries);
        }   
        plot.setDataset(xyseriescollection);
    }
    
}
